﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace TangoFamilyAndGuide.Site
{
    public partial class HousingConsulta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();

                if (Funciones.SessionTimeOut(Session))
                {
                    Response.Redirect("~/SessionTimeout.aspx");
                }
                else
                {
                    Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                    List<Entidades.Housing> lista = new List<Entidades.Housing>();
                    lista = RN.Housing.LeerListaHousingsPorUsuario(sesion);
                    HousingsGridView.DataSource = lista;
                    ViewState["Housings"] = lista;
                    HousingsGridView.DataBind();
                    if (lista.Count == 0)
                    {
                        MensajeLabel.Text = "No Milonguero Housing Network associated with this user.";
                    }
                }
            }
        }
        protected void HousingsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton verLinkButton = (LinkButton)e.Row.FindControl("VerLinkButton");
                verLinkButton.CommandArgument = e.Row.RowIndex.ToString();
                if (e.Row.Cells[7].Text != "Active")
                {
                    e.Row.ForeColor = Color.Red;
                }
            }
        }
        protected void HousingsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Ver" && e.CommandArgument != null)
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                List<Entidades.Housing> lista = (List<Entidades.Housing>)ViewState["Housings"];
                Entidades.Housing Housing = lista[rowIndex];

                IdHousingTextBox.Text = Housing.Id.ToString();
                DescripcionTextBox.Text = Housing.Descripcion;
                Domicilio.Direccion = Housing.Direccion;
                Domicilio.Pais = Housing.Pais;
                Domicilio.Provincia = Housing.Provincia;
                Domicilio.Localidad = Housing.Localidad;
                Domicilio.CodPost = Housing.CodPost;
                Contacto.Nombre = Housing.Contacto.Nombre;
                Contacto.Email = Housing.Contacto.Email;
                Contacto.Telefono = Housing.Contacto.Telefono;
                GoogleMapTextBox.Text = Housing.GoogleMap;
                ComentariosTextBox.Text = Housing.Comentarios;

                IdHousingTextBox.Enabled = false;
                DescripcionTextBox.Enabled = false;
                Domicilio.Enabled = false;
                Contacto.Enabled = false;
                GoogleMapTextBox.Enabled = false;
                ComentariosTextBox.Enabled = false;

                AjaxControlToolkit.ModalPopupExtender modalPopupExtender1 = (AjaxControlToolkit.ModalPopupExtender)HousingsGridView.Rows[rowIndex].FindControl("ModalPopupExtender1");
                modalPopupExtender1.Show();
            }
            else if (e.CommandName == "VerDetalle" && e.CommandArgument != null)
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                List<Entidades.Housing> lista = (List<Entidades.Housing>)ViewState["Housings"];
                Entidades.Housing Housing = lista[rowIndex];
                Session["Housing"] = Housing;
                Response.Redirect("~/HousingGuideConsultaDetallada.aspx?Id");
            }
        }
    }
}